package com.icloud.admin.resource;

import com.icloud.admin.application.AccessApplicationService;
import com.icloud.admin.application.command.ChangeResourceCommand;
import com.icloud.admin.query.AccessQueryService;
import com.icloud.admin.application.command.CreateResourceCommand;
import com.icloud.admin.query.dto.ResourceDTO;
import com.icloud.common.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/resources")
public class ResourceController {

    @Autowired
    private AccessQueryService accessQueryService;
    @Autowired
    private AccessApplicationService accessApplicationService;

    /**
     * 查询所有资源
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public InvokeResult findAll() {
        return new InvokeResult(accessQueryService.findResourceAll());
    }

    /**
     * 创建资源
     *
     * @param createAccessUrlCommand
     */
    @RequestMapping(method = RequestMethod.POST)
    public InvokeResult createResource(@Valid @RequestBody CreateResourceCommand createAccessUrlCommand) {
        accessApplicationService.createResource(createAccessUrlCommand);
        return new InvokeResult();
    }

    /**
     * 删除资源
     *
     * @param resourceId
     */
    @RequestMapping(value = "/{resourceId}", method = RequestMethod.DELETE)
    public InvokeResult removeRole(@PathVariable("resourceId") String resourceId) {
        accessApplicationService.removeResource(resourceId);
        return new InvokeResult();
    }

    /**
     * 更新资源
     *
     * @param resourceId
     * @param changeResourceCommand
     */
    @RequestMapping(value = "/{resourceId}", method = RequestMethod.PUT)
    public InvokeResult changeAccessUrlPops(@PathVariable("resourceId") String resourceId, @Valid @RequestBody ChangeResourceCommand changeResourceCommand) {
        changeResourceCommand.setId(resourceId);
        accessApplicationService.changeResource(changeResourceCommand);
        return new InvokeResult();
    }

}
